﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using TagThisOrgProject.Models;
using WebMatrix.WebData;

namespace TagThisOrgProject.Repository
{
    public class UserRepository
    {
        private TagContext Context { get; set; }
        public UserRepository(TagContext context)
        {
            this.Context = context;
        }

        internal UserProfile GetUser(string UserName)
        {
            var user = (from u in Context.UserProfiles
                       where u.UserName == UserName
                       select u).FirstOrDefault();
            return user;
        }
        internal UserProfile GetUser(int id)
        {
                return (from u in Context.UserProfiles
                        where u.UserId == id
                        select u).FirstOrDefault();
        }

        internal Int32 GetUserId(String UserName)
        {
            int r = 0;
            try
            {
               r = WebSecurity.GetUserId(UserName);
            }
            catch (Exception e)
            {
                throw new Exception("Не удалось получить идентефикатор пользователя. Код 0020");
            }

            return r;
        }
        internal Int32 GetCurentUserId()
        {
            return WebSecurity.CurrentUserId;
        }
        internal UserProfile GetCurrentUser()
        {
            return GetUser(GetCurentUserId());
        }

    }
}